package io.renren.common.result;

/**
 * @author Luo Sheng
 * @school YiBin university
 * @create 2023-02-22 10:41
 */

import lombok.Data;

/**
 * description：统一的返回结果类
 */
@Data
public class Result<T> {

    private Integer code; // 状态码

    private String msg; // 返回状态信息（成功/失败）

    private T data; // 返回前端的数据对象

    public Result() {
    }

    /**
     * 成功方法
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Result<T> success(T data){
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(ResultCodeEnum.SUCCESS.getMessage());
        if (data != null){
            result.data = data;
        }
        return result;
    }

    /**
     * 失败方法
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Result<T> fail(T data){
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.FAIL.getCode());
        result.setMsg(ResultCodeEnum.FAIL.getMessage());
        if (data != null){
            result.data = data;
        }
        return result;
    }

    public Result<T> message(String msg){
        this.setMsg(msg);
        return this;
    }

    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }


}
